#pragma once
#include "aot_inet_config.h"
#include <ace/SOCK_Dgram.h>
#include <interface/aot_inet_interface.h>

class udp_syn_channel_impl : public aot::inet::iudp_syn_channel
{
public:
	udp_syn_channel_impl(void);
	virtual ~udp_syn_channel_impl(void);
public:
	virtual bool open(int port, const char* ip = "0.0.0.0");
	virtual void close();

	virtual int send(aot::inet::inet_addr_t* remote_addr, aot::inet::ipacket* pkt, const aot::inet::inet_time_t* tm_out = NULL);
	virtual int send(int port, const char* ip, aot::inet::ipacket* pkt, const aot::inet::inet_time_t* tm_out = NULL);

	virtual int recv(aot::inet::ipacket** pkt, aot::inet::inet_addr_t* remote_addr, const aot::inet::inet_time_t* tm_out = NULL);
	
	virtual void set_sock_send_buf_size(int n);
	virtual void set_sock_recv_buf_size(int n);
private:
	int send_i(const ACE_INET_Addr& remote_addr, aot::inet::ipacket* pkt, const aot::inet::inet_time_t* tm_out);
	int sendv_i(ACE_Message_Block* mb, const ACE_INET_Addr& remote_addr);
	void close_i();
	int mb_to_iovec(iovec v[], int num, ACE_Message_Block* mb);
	size_t update_mb(size_t send_len, ACE_Message_Block* mb);
private:
	ACE_SOCK_Dgram sock_dgram_;
	bool is_opened_;
	ACE_INT32 so_send_buf_size_;
	ACE_INT32 so_recv_buf_size_;
};
